//
//  NSString+Font.m
//  OMTCoreKit
//
//  Created by 昭荣伊 on 2018/7/27.
//

#import "NSString+Font.h"

@implementation NSString (Font)

- (CGSize)omt_sizeWithFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    NSMutableDictionary *attr = [NSMutableDictionary new];
    attr[NSFontAttributeName] = font;
    if (lineBreakMode != NSLineBreakByWordWrapping) {
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineBreakMode = lineBreakMode;
        attr[NSParagraphStyleAttributeName] = paragraphStyle;
    }
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:attr context:nil];
    result = rect.size;
    return result;
}

- (CGSize)omt_sizeWithFont:(UIFont *)font width:(CGFloat)width {
    return [self omt_sizeWithFont:font size:CGSizeMake(width, HUGE)
                             mode:NSLineBreakByWordWrapping];
}

- (CGSize)omt_sizeWithFont:(UIFont *)font height:(CGFloat)height {
    return [self omt_sizeWithFont:font size:CGSizeMake(HUGE, height)
                             mode:NSLineBreakByWordWrapping];
}

- (CGFloat)omt_widthWithFont:(UIFont *)font {
    CGSize size = [self omt_sizeWithFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

- (CGFloat)omt_heightWithFont:(UIFont *)font{
    CGSize size = [self omt_sizeWithFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}

@end
